package com.watering.plan;


import java.util.Calendar;
import java.util.StringTokenizer;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.widget.Toast;


public class NotificationService extends Service {
	NotificationManager mNM;
	Base bs;
	Calendar cal;
	
	
	@Override
    public void onCreate()  {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // show the icon in the status bar
       
        Toast.makeText(NotificationService.this, R.string.hello,
                Toast.LENGTH_LONG).show();
        
         bs = new Base(this);
        
        // starting thread
        Thread thr = new Thread(null, mTask, "Notification_Service");
        thr.start();
        
    }

    @Override
    public void onDestroy() {

        mNM.cancel(R.string.service_started);

        // Tell the user we stopped.
     //   Toast.makeText(this, R.string.service_finished, Toast.LENGTH_SHORT).show();
    }

    /**
     * The function that runs in our worker thread
     */
   
    Runnable mTask = new Runnable() {
        public void run() {
		 cal = Calendar.getInstance();	
			 
		int hour =  cal.get(Calendar.HOUR_OF_DAY );
			int minute = cal.get(Calendar.MINUTE);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			int month = cal.get(Calendar.MONTH);
			

            long endTime = System.currentTimeMillis() + 24*60*60*1000;
           
            while (System.currentTimeMillis() < endTime) {
                synchronized (mBinder) {
                    try {
                        mBinder.wait(endTime - System.currentTimeMillis());
                    } catch (Exception e) {
                    }
                }
            }
            
            showNotification();
            
            endTime = System.currentTimeMillis() + 30*1000;
            while (System.currentTimeMillis() < endTime) {
                synchronized (mBinder) {
                    try {
                        mBinder.wait(endTime - System.currentTimeMillis());
                    } catch (Exception e) {
                    }
                }
            }
            // Done with our work...  stop the service!
            NotificationService.this.stopSelf();
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    
    private void showNotification() {
    	
        CharSequence text = getText(R.string.service_started);
        Context context = getApplicationContext(); 

        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, CheckPlants.class), 0);
        
        notification.setLatestEventInfo(context, getText(R.string.service_label),
                       text, contentIntent);

        mNM.notify(R.string.service_started, notification);
    }

    private final IBinder mBinder = new Binder() {
        @Override
		protected boolean onTransact(int code, Parcel data, Parcel reply,
		        int flags) throws RemoteException {
            return super.onTransact(code, data, reply, flags);
        }
    };
    
}

